/**
 * Created by Darkness on 14-1-6.
 */
(function($) {
    var arkflow = $.arkflow;

    // 创建路径对象
    arkflow.PathArrow = Base.extend({
        constructor : function(path) {
            this.path = path;
            this.paper = path.paper;
            this.comp = this.paper.path(path.pathConfig.attr.arrow.path).attr(path.pathConfig.attr.arrow);
            this.node = this.comp.node;
        },
        draw: function() {
            if (!this.path.linePoints.hasPoint()) {
                return "";
            }
            var lastPoint = this.path.linePoints.lastPoint();
            var arrowArry = this.arrow(lastPoint.left().pos(), lastPoint.pos(), this.path.pathConfig.attr.arrow.radius);
            var str = "M" + arrowArry[0].x + " " + arrowArry[0].y
                + "L" + arrowArry[1].x + " " + arrowArry[1].y
                + "L" + arrowArry[2].x + " " + arrowArry[2].y + "z";

            this.comp.attr({
                path : str
            });
        },
        //箭头
        arrow : function(leftPoint, rightPoint, radius) {
            var area = Math.atan2(leftPoint.y - rightPoint.y, rightPoint.x - leftPoint.x) * (180 / Math.PI);

            var x = rightPoint.x - radius * Math.cos(area * (Math.PI / 180));
            var y = rightPoint.y + radius * Math.sin(area * (Math.PI / 180));

            var x1 = x + radius * Math.cos((area + 120) * (Math.PI / 180));
            var y1 = y - radius * Math.sin((area + 120) * (Math.PI / 180));
            var x2 = x + radius * Math.cos((area + 240) * (Math.PI / 180));
            var y2 = y - radius * Math.sin((area + 240) * (Math.PI / 180));

            return [rightPoint, {
                x : x1,
                y : y1
            }, {
                x : x2,
                y : y2
            }];
        },
        attr: function(p) {
            this.comp.attr(p);
        },
        remove:function() {
            this.comp.remove();
        }
    });
})(jQuery);
